#!/bin/bash
# This script creates symbolic link in JBoss deploy directory pointing to exploded EAR

CURR_DIR=`pwd`

# Switch to script's directory
    BASEDIR=$(dirname $0)
    #echo $BASEDIR
    cd $BASEDIR

touch=0
war_extended=0

until [ -z "$1" ];do
    if [ "$1" = "-touch" ];then
        touch=1
    fi
    if [ "$1" = "-war_extended" ];then
        war_extended=1
    fi
    if [ "$1" = "-help" -o "$1" = "-?" ];then
        echo "Usage: $0 [-touch] [-war_extended] [-symlinks]"
        exit
    fi
    shift
done

. ./setupEnv.sh

CopyFiles()
{
    CURR_DIR2=`pwd`
    # Copy fresh files from WEB project source directory
    if [ -d $1 ];then
        cd $1
        echo "############################################################"
        echo "# Copying $1 to $2 (mask $3)"
        echo "############################################################"
        for f in `find . -type f | grep -v .svn | grep -E "$3"`
        do
         echo "    Processing $f"
         echo "        Copying fresh copy from source directory"
         mkdir -p `dirname $2/$f`
         cp $1/$f $2/$f
        done
    
        cd $CURR_DIR2
        echo ""
        echo ""
    else
        echo "############################################################"
        echo "############################################################"
        echo "############################################################"
        echo ""
        echo ""
        echo "WARNING WARNING WARNING: Source directory $1 does not exist!!!"
        echo ""
        echo ""
        echo "############################################################"
        echo "############################################################"
        echo "############################################################"
    fi
}

    MYDIR=`pwd`

    EXPLODED_EAR_DIR=$MYDIR/ear/target/DataModelling
    #WAR_ARCHIVE_NAME=DataModelling-web-gui-1.0.0-SNAPSHOT.war
    #EJB_ARCHIVE_NAME=DataModelling-ejb-backend-1.0.0-SNAPSHOT.jar
    WAR_ARCHIVE_NAME=DataModelling-web-gui.war
    EJB_ARCHIVE_NAME=DataModelling-ejb-backend.jar

    WAR_1_SRC=$MYDIR/web-gui/src/main/webapp
    WAR_1_EXPLODED_DIR=$EXPLODED_EAR_DIR/$WAR_ARCHIVE_NAME
    WAR_2_SRC=$MYDIR/web-gui/src/main/resources
    WAR_2_EXPLODED_DIR=$EXPLODED_EAR_DIR/$WAR_ARCHIVE_NAME/WEB-INF/classes
    WAR_3_SRC=$MYDIR/web-gui/target/classes
    WAR_3_EXPLODED_DIR=$EXPLODED_EAR_DIR/$WAR_ARCHIVE_NAME/WEB-INF/classes

    EJB_1_SRC=$MYDIR/ejb-backend/src/main/resources
    EJB_1_EXPLODED_DIR=$EXPLODED_EAR_DIR/$EJB_ARCHIVE_NAME
    EJB_2_SRC=$MYDIR/ejb-backend/target/classes
    EJB_2_EXPLODED_DIR=$EXPLODED_EAR_DIR/$EJB_ARCHIVE_NAME

CopyFiles $WAR_1_SRC $WAR_1_EXPLODED_DIR "xhtml$|jsp$|css$"
CopyFiles $WAR_2_SRC $WAR_2_EXPLODED_DIR "properties$"

if [ $war_extended -gt 0 ];then
    CopyFiles $WAR_1_SRC/WEB-INF $WAR_1_EXPLODED_DIR/WEB-INF "xml$"
    #CopyFiles $WAR_3_SRC $WAR_3_EXPLODED_DIR "class$"
fi

#CopyFiles $EJB_1_SRC $EJB_1_EXPLODED_DIR "properties$|xml$"
#CopyFiles $EJB_2_SRC $EJB_2_EXPLODED_DIR "class$"

if [ $touch -gt 0 ];then
    APPL_XML=$EXPLODED_EAR_DIR/META-INF/application.xml
    echo "############################################################"
    echo "# Touching $APPL_XML"
    echo "############################################################"
    touch $APPL_XML
fi

# and return back
cd $CURR_DIR
